package com.jiazhong.basic.borrow.dao;

import com.jiazhong.basic.borrow.db.DBManager;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class BorrowDAO {
    private Connection conn;
    private PreparedStatement ps;

    @SneakyThrows
    public int borrow(int bookId, int userId) {
        conn = DBManager.getDruidConnection();
        String sql = "insert into borrow values(null,?,?,now(),null)";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, bookId);
        return ps.executeUpdate();
    }

    @SneakyThrows
    public int back(int bookId, int userId) {
        conn = DBManager.getDruidConnection();
        String sql = "update borrow set back_date=now() where user_id=? and book_id=? and back_date is null";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, userId);
        ps.setInt(2, bookId);
        return ps.executeUpdate();
    }
}
